package labox.innovation.gameserver.model;

import labox.innovation.gameserver.templates.StatsSet;
import labox.innovation.util.Rnd;

/**
 * 触发型技能的 触发条件  (被动型触发的技能)
 * @author  zjj
 */
public final class ChanceCondition {
	public static final int EVT_HIT = 1;

	public static final int EVT_CRIT = 2;

	public static final int EVT_CAST = 4;

	public static final int EVT_PHYSICAL = 8;

	public static final int EVT_MAGIC = 16;

	public static final int EVT_MAGIC_GOOD = 32;

	public static final int EVT_MAGIC_OFFENSIVE = 64;

	public static final int EVT_ATTACKED = 128;

	public static final int EVT_ATTACKED_HIT = 256;

	public static final int EVT_ATTACKED_CRIT = 512;

	public static final int EVT_HIT_BY_SKILL = 1024;

	public static final int EVT_HIT_BY_OFFENSIVE_SKILL = 2048;

	public static final int EVT_HIT_BY_GOOD_MAGIC = 4096;

	public static final int EVT_EVADED_HIT = 8192;

	public static enum TriggerType {
		// You hit an enemy
		ON_HIT(1),
		// You hit an enemy - was crit
		ON_CRIT(2),
		// You cast a skill
		ON_CAST(4),
		// You cast a skill - it was a physical one
		ON_PHYSICAL(8),
		// You cast a skill - it was a magic one
		ON_MAGIC(16),
		// You cast a skill - it was a magic one - good magic
		ON_MAGIC_GOOD(32),
		// You cast a skill - it was a magic one - offensive magic
		ON_MAGIC_OFFENSIVE(64),
		// You are attacked by enemy
		ON_ATTACKED(128),
		// You are attacked by enemy - by hit
		ON_ATTACKED_HIT(256),
		// You are attacked by enemy - by hit - was crit
		ON_ATTACKED_CRIT(512),
		// A skill was casted on you
		ON_HIT_BY_SKILL(1024),
		// An evil skill was casted on you
		ON_HIT_BY_OFFENSIVE_SKILL(2048),
		// A good skill was casted on you
		ON_HIT_BY_GOOD_MAGIC(4096),
		// Evading melee attack
		ON_EVADED_HIT(8192);

		private final int _mask;

		private TriggerType(int mask) {
			_mask = mask;
		}

		public final boolean check(int event) {
			return (_mask & event) != 0; // Trigger (sub-)type contains event (sub-)type
		}
	}

	private final TriggerType _triggerType; //触发的技能类型

	private final int _chance; //触发几率

	private final boolean _pvpOnly; //是否仅对pvp有效

	private ChanceCondition(TriggerType trigger, int chance, boolean pvpOnly) {
		_triggerType = trigger;
		_chance = chance;
		_pvpOnly = pvpOnly;
	}

	public static ChanceCondition parse(StatsSet set) {
		try {
			TriggerType trigger = set.getEnum("chanceType", TriggerType.class, null);
			int chance = set.getInteger("activationChance", 0);
			boolean pvpOnly = set.getBool("pvpChanceOnly", false);

			if (trigger != null && chance > 0)
				return new ChanceCondition(trigger, chance, pvpOnly);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ChanceCondition parse(String chanceType, int chance, String elements, boolean pvpOnly) {
		try {
			if (chance <= 0 || chanceType == null)
				return null;

			TriggerType trigger = Enum.valueOf(TriggerType.class, chanceType);

			if (trigger != null)
				return new ChanceCondition(trigger, chance, pvpOnly);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public boolean trigger(int event, boolean playable) {
		if (_pvpOnly && !playable)
			return false;

		return _triggerType.check(event) && Rnd.get(100) < _chance;

	}

	@Override
	public String toString() {
		return "Trigger[" + _chance + ";" + _triggerType.toString() + "]";
	}
}